Google APIを利用して、2地点間を結ぶ直線の標高を取得
library(rjson)
library(RCurl)
## Loading required package: bitops
住所ベクトルからを緯度経度を取得する関数
get.coords <- function(addresses) {
# Google APIで住所からJSONを取得
get.url <- function(address) {
address <- curlEscape(iconv(address, to = "UTF-8"))
url <- paste("http://maps.googleapis.com/maps/api/geocode/json?address=",
address, "&sensor=false®ion=JP&language=ja", sep = "")
getURL(url)
}
# JSONから緯度経度を抽出
get.coord <- function(json) {
lat = fromJSON(json)$results[[1]]$geometry$location$lat
lon = fromJSON(json)$results[[1]]$geometry$location$lng
c(latitude = lat, longitude = lon)
}
# 上の2つの関数を住所ベクトルに適用
jsons <- sapply(addresses, get.url)
t(sapply(jsons, get.coord))
}
住所ベクトルから緯度経度を取得
addresses <- c("広島県広島市南区宇品東1-1-71", "広島県東広島市鏡山1-3-1")
coords <- get.coords(addresses)
coords
## latitude longitude
## 広島県広島市南区宇品東1-1-71 34.37 132.5
## 広島県東広島市鏡山1-3-1 34.40 132.7
2地点を結ぶ直線の標高を取得
n.sample <- 10
path <- paste(paste(coords[1, ], collapse = ","), paste(coords[2, ], collapse = ","),
sep = "|")
url <- paste("http://maps.googleapis.com/maps/api/elevation/json?", "path=",
path, "&samples=", n.sample, "&sensor=false", sep = "")
rjson <- fromJSON(getURL(url))
ext.path <- function(x) {
ele = rjson$results[[x]]$elevation
lat = rjson$results[[x]]$location$lat
lon = rjson$results[[x]]$location$lng
c(elevation = ele, latitude = lat, longitude = lon)
}
coords <- t(sapply(1:n.sample, ext.path))
coords
## elevation latitude longitude
## [1,] 2.774 34.37 132.5
## [2,] 19.552 34.37 132.5
## [3,] 7.001 34.37 132.5
## [4,] 11.973 34.38 132.6
## [5,] 347.773 34.38 132.6
## [6,] 490.916 34.39 132.6
## [7,] 319.536 34.39 132.6
## [8,] 276.481 34.40 132.7
## [9,] 230.736 34.40 132.7
## [10,] 231.137 34.40 132.7